﻿package org.xhs.report.common.adv
{
    import baidu.lib.utils.*;
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.utils.*;
    import flash.display.*;
    import flash.events.*;

    public class AdvBuffer extends EventDispatcher
    {
        private var _conainer:DisplayObjectContainer;
        private var advBgSpt:Sprite;
        private var advBgMidLayerWidth:Number = 0;
        private var advBgMidLayerHeight:Number = 0;
        private var advMask:Sprite;
        private var advBodyBg:Sprite;
        private var advUrl:String;
        private var advLink:String;
        private var advTime:Number = 5;
        private var countDown:CountDown;
        private var advBody:AdvLoader;
        private var _advSuccess:Boolean = false;
        private var _hasClicked:Boolean = false;
        private var screenModel:ScreenModel;
        private var advFeModel:AdvFeModel;
        private var advRdModel:AdvRdModel;
        private var _showing:Boolean = false;
        private var advHeight:Number = 774;
        private const ADV_WIDTH:Number = 660;

        public function AdvBuffer(param1:DisplayObjectContainer)
        {
            this._conainer = param1;
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.advFeModel = InstanceManager.createSingletonInstance(AdvFeModel);
            if (this.advFeModel.hasAdv)
            {
                this.advRdModel = InstanceManager.createSingletonInstance(AdvRdModel);
                this.advRdModel.addEventListener(AdvRdModel.ADV_ERROR, this.advVanish);
                this.advRdModel.addEventListener(AdvRdModel.ADV_COMPLETE, this.createAdv);
                this.advRdModel.advDataUrl = this.advFeModel.advUrl;
                this.advRdModel.init();
            }
            return;
        }// end function

        private function createAdv(event:Event = null) : void
        {
            this.advUrl = this.advRdModel.advUrl;
            this.advLink = this.advRdModel.advLink;
            this.advHeight = this.advRdModel.advHeight * this.ADV_WIDTH / this.advRdModel.advWidth ? (this.advRdModel.advHeight * this.ADV_WIDTH / this.advRdModel.advWidth) : (this.advHeight);
            this.advBgSpt = new Sprite();
            this.advBgSpt.x = 1;
            this.advBgSpt.y = Config.TB_HEIGHT;
            this.advBgMidLayerWidth = this.screenModel.stageWidth - Config.DV_WIDTH_RIGHT_SCROLLBAR - 2;
            this.advBgMidLayerHeight = this.screenModel.stageHeight - this.advBgSpt.y - Config.SB_HEIGHT;
            this.advBgSpt.graphics.clear();
            this.advBgSpt.graphics.beginFill(15632384, 0);
            this.advBgSpt.graphics.drawRect(-this.advBgSpt.x, -this.advBgSpt.y, this.screenModel.stageWidth, this.screenModel.stageHeight);
            this.advBgSpt.graphics.beginFill(15658734, 0.95);
            this.advBgSpt.graphics.drawRect(0, 0, this.advBgMidLayerWidth, this.advBgMidLayerHeight);
            this.advBgSpt.graphics.endFill();
            this._conainer.addChild(this.advBgSpt);
            this.advBodyBg = new Sprite();
            this.advBodyBg.x = 1;
            this.advBodyBg.y = Config.TB_HEIGHT;
            this.advBodyBg.mouseChildren = false;
            this.advBodyBg.buttonMode = true;
            this.advBodyBg.addEventListener(MouseEvent.MOUSE_UP, this.advClick);
            this._conainer.addChild(this.advBodyBg);
            this.advMask = new Sprite();
            this.advMask.graphics.clear();
            this.advMask.graphics.beginFill(16711680);
            this.advMask.graphics.drawRect(0, 0, this.ADV_WIDTH, this.advHeight);
            this.advMask.graphics.endFill();
            this.advBody = new AdvLoader();
            this.advBody.advImageUrl = this.advUrl;
            this.advBody.advLinkUrl = this.advLink;
            this.advBody.advDuration = this.advTime;
            this.advBody.advWidth = this.ADV_WIDTH;
            this.advBody.addEventListener(AdvLoader.FINISH_LOADING, this.advComplete);
            this.advBody.addEventListener(AdvLoader.FAIL_LOADING, this.advFailed);
            this.advBody.addEventListener(AdvLoader.ADV_VANISH, this.advVanish);
            this.advBody.popAdv();
            this.countDown = new CountDown(this.advTime);
            this.countDown.start();
            this.advBody.x = 20;
            this.advBody.y = 20;
            this.advMask.x = this.advBody.x;
            this.advMask.y = this.advBody.y;
            this.advMask.height = Math.min(this.advMask.height, this.advBgMidLayerHeight - this.advMask.y);
            this.countDown.x = this.advMask.x + this.advMask.width - this.countDown.width;
            this.countDown.y = this.advBody.y;
            this.advBodyBg.addChild(this.countDown);
            this.screenModel.addEventListener(Event.RESIZE, this.screenResize);
            this._showing = true;
            return;
        }// end function

        private function advClick(event:Event) : void
        {
            if (!this._hasClicked)
            {
                if (this.advLink)
                {
                    BrowserUtil.openNewWindow(this.advLink);
                }
            }
            this._hasClicked = true;
            return;
        }// end function

        private function advComplete(event:Event) : void
        {
            this._advSuccess = true;
            this.advBodyBg.graphics.clear();
            this.advBodyBg.graphics.beginFill(16777215);
            this.advBodyBg.graphics.drawRect(20, 20, this.ADV_WIDTH, Math.min(this.advBgMidLayerHeight - Config.TB_HEIGHT, this.advBody.height));
            this.advBodyBg.graphics.endFill();
            this.advMask.height = Math.min(this.advMask.height, this.advBgMidLayerHeight - this.advMask.y);
            this.advBodyBg.addChild(this.advBody);
            this.advBodyBg.addChild(this.advMask);
            this.advBody.mask = this.advMask;
            this.advBodyBg.addChild(this.countDown);
            return;
        }// end function

        private function advFailed(event:Event) : void
        {
            this._advSuccess = false;
            return;
        }// end function

        private function advVanish(event:Event = null) : void
        {
            if (this._advSuccess)
            {
                WenkuLog.addLog(WenkuLog.LOGURL_AD_SUCCESS + "&uid=" + this.advFeModel.uid + "&doc_id=" + this.advFeModel.docId + "&doc_title=" + encodeURI("[文库]" + this.advFeModel.docTitle));
            }
            else
            {
                WenkuLog.addLog(WenkuLog.LOGURL_AD_FAILED + "&uid=" + this.advFeModel.uid + "&doc_id=" + this.advFeModel.docId + "&doc_title=" + encodeURI("[文库]" + this.advFeModel.docTitle));
            }
            this.advRdModel.removeEventListener(AdvRdModel.ADV_COMPLETE, this.createAdv);
            this.screenModel.removeEventListener(Event.RESIZE, this.screenResize);
            if (this.advBgSpt)
            {
                if (this._conainer.contains(this.advBgSpt))
                {
                    this._conainer.removeChild(this.advBgSpt);
                }
                this.advBgSpt = null;
            }
            if (this.advBodyBg)
            {
                this.advBodyBg.removeEventListener(MouseEvent.MOUSE_UP, this.advClick);
                if (this._conainer.contains(this.advBodyBg))
                {
                    this._conainer.removeChild(this.advBodyBg);
                }
                this.advBodyBg = null;
            }
            if (this.advMask)
            {
                this.advMask = null;
            }
            if (this.advBody)
            {
                this.advBody.removeEventListener(AdvLoader.FINISH_LOADING, this.advComplete);
                this.advBody.removeEventListener(AdvLoader.ADV_VANISH, this.advVanish);
                this.advBody = null;
            }
            this._showing = false;
            return;
        }// end function

        private function screenResize(event:Event = null) : void
        {
            if (this.screenModel.isFullscreen)
            {
                this.advVanish(null);
            }
            else
            {
                this.advBgMidLayerWidth = this.screenModel.stageWidth - Config.DV_WIDTH_RIGHT_SCROLLBAR - 2;
                this.advBgMidLayerHeight = this.screenModel.stageHeight - this.advBgSpt.y - Config.SB_HEIGHT;
                this.advBgSpt.graphics.clear();
                this.advBgSpt.graphics.beginFill(15632384, 0);
                this.advBgSpt.graphics.drawRect(-this.advBgSpt.x, -this.advBgSpt.y, this.screenModel.stageWidth, this.screenModel.stageHeight);
                this.advBgSpt.graphics.beginFill(15658734, 0.95);
                this.advBgSpt.graphics.drawRect(0, 0, this.advBgMidLayerWidth, this.advBgMidLayerHeight);
                this.advBgSpt.graphics.endFill();
                this.advMask.graphics.clear();
                this.advMask.graphics.beginFill(16711680);
                this.advMask.graphics.drawRect(0, 0, this.ADV_WIDTH, this.advHeight);
                this.advMask.graphics.endFill();
                this.advBodyBg.graphics.clear();
                this.advBodyBg.graphics.beginFill(16777215);
                this.advBodyBg.graphics.drawRect((this.advBgMidLayerWidth - this.ADV_WIDTH) / 2, 20, this.ADV_WIDTH, Math.min(this.advBgMidLayerHeight - Config.TB_HEIGHT, this.advBody.height));
                this.advBodyBg.graphics.endFill();
                this.advMask.height = Math.min(this.advHeight, this.advBgMidLayerHeight - this.advMask.y);
            }
            return;
        }// end function

        public function get showing() : Boolean
        {
            return this._showing;
        }// end function

    }
}
